package com.g2room.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import com.g2room.myuall.grap.ky.model.po.PeeUnivInfo;
import com.g2room.myuall.util.DbUtil;

public class HandleDb {
	public static void main(String[] args) {
		Map<String, String> univs = getSchoolInfo();
		Map<Integer, String> artis = getArti();
		for(Integer id : artis.keySet()) {
			delFromErrDb(univs.get(artis.get(id)), id);
		}
	}
	

	public static void delFromErrDb(String name, int id) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn().prepareStatement("update pee_arti set school_name=? where id=?");
			grapPs.setString(1, name);
			grapPs.setInt(2, id);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	public static Map<Integer, String> getArti() {
		Map<Integer, String> univs = new HashMap<Integer, String>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select id,school_code from pee_arti";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				PeeUnivInfo univ = new PeeUnivInfo();
				univs.put(rs.getInt("id"), rs.getString("school_code"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}

	public static Map<String, String> getSchoolInfo() {
		Map<String, String> univs = new HashMap<String, String>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select code,name from pee_univ_info";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				PeeUnivInfo univ = new PeeUnivInfo();
				univs.put(rs.getString("code"), rs.getString("name"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}
}
